import logging
import argparse
from pathlib import Path
from typing import Any, Generator
from pydantic import BaseModel
from pysam import FastaFile  # pylint: disable=E0611

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger("Prepare File")


class PrepareBase(BaseModel):
    reference_file: Path
    outdir: Path
    step: int = 100 * 1000

    def iter_reference(self) -> Generator[tuple[str, int, list[tuple[int, int]]], Any, None]:
        with FastaFile(str(self.reference_file)) as reader:
            for chrom, size in filter(lambda x: len(x[0]) <= 5 and x[0] not in ["M", "MT", "chrM"], zip(reader.references, reader.lengths)):
                yield chrom, size, list(map(lambda x: (x, x + self.step), range(0, size, self.step)))

    def run(self):
        raise NotImplementedError("Implement required")


def set_parser(parser: argparse.ArgumentParser):
    parser.add_argument("--reference_file", "-r", type=Path, required=True, help="input, reference file")
    parser.add_argument("--outdir", "-o", type=Path, required=True, help="output, output directory")
    parser.add_argument("--step", "-s", type=int, default=1000 * 1000, help="parameter, step size for every sub files")
